﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

using YouHP.IDAL;
using YouHP.Model;
using YouHP.Model.Group;

namespace YouHP.SqlServerDAL
{
    public class GroupTypeDAL : IGroupType
    {
        #region basic operations
        public bool AddGroupType(GroupTypeInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.GroupTypeSP_AddGroupType,
                info.GroupTypeName,
                info.DiseaseType,
                info.Introduction,
                info.Logo,
                info.GroupNum
                ) > 0;
        }

        public bool UpdateGroupType(GroupTypeInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.GroupTypeSP_UpdateGroupType,
                info.Id,
                info.GroupTypeName,
                info.Introduction,
                info.Logo
                ) > 0;
        }

        public bool DeleteGroupType(int[] ids)
        {
            int cnt = ids.Length;
            if (cnt <= 0)
                return false;

            StringBuilder sb = new StringBuilder();
            sb.Append("[id] in (");
            sb.Append(ids[0]);

            for (int i = 1; i < cnt; i++)
            {
                sb.Append(", ");
                sb.Append(ids[i]);
            }
            sb.Append(")");

            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.GroupTypeSP_DelGroupType,
                sb.ToString()) >= cnt;
        }

        public GroupTypeInfo GetGroupTypeInfo(int id)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.GroupTypeSP_GetGroupType,
                id) as IDataReader;
            return CBO.FillObject<GroupTypeInfo>(reader);
        }

        public static GroupTypeInfo GetGroupTypeInfoByDiseaseType(int dtId)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.GroupTypeSP_GetGroupTypeByDiseaseType,
                dtId) as IDataReader;
            return CBO.FillObject<GroupTypeInfo>(reader);
        }
        #endregion

        #region list and search operations
        private List<GroupTypeInfo> GetGroupTypeList(int pageSize, int pageIndex, string condstr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                Common.GetFullName(Common.GroupTypeSP_GetGroupTypeList),
                pageSize, pageIndex, condstr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }
            dr = dset.Tables[1].CreateDataReader();

            return CBO.FillCollection<GroupTypeInfo>(dr);
        }

        public List<GroupTypeInfo> GetGroupTypeList(int pageSize, int pageIndex, out int count)
        {
            return GetGroupTypeList(pageSize, pageIndex, "", " [Id] desc ", out count);
        }

        public List<GroupTypeInfo> GetGroupTypeByName(int pageSize, int pageIndex, string keyword, out int count)
        {
            string condStr = string.Format(@" [GroupTypeName] like '%{0}%' ", keyword); 
            return GetGroupTypeList(pageSize, pageIndex, condStr, " [Id] desc ", out count);
        }
        #endregion
    }
}

